package cn.cool.basic;


import cn.cool.common.CdCheckResult;
import cn.cool.config.CdConfigSetting;
import cn.cool.util.CdLogicCheckUtil;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

public class CdDropDownOperate {

    private static final String CONST_CD_KEY = "cdKey";
    private static final String CONST_CD_VALUE = "cdValue";
    public static final String CONST_VAL_DATA_KEY = "val_data";
    public static final String CONST_VAL_COMMENT_KEY = "val_comment";
    public static final String CONST_VAL_ORIGINAL_SEQ_KEY = "val_org_seq";
    private static boolean IS_INIT_DATA = false;

    private static final Map<String, Object> cdCoreDataMap = new ConcurrentHashMap<>();

    private CdDropDownOperate() {
    }

    public static void initSettingData() {
        if (IS_INIT_DATA) return;

        List<Map<String, String>> fileDataList = new ArrayList<>();

        // 获取数据
        getResourceFileData(fileDataList, null);

        // 导入数据
        importFileData(fileDataList);
    }

    public static void initSettingData(CdConfigSetting configSet) {
        if (IS_INIT_DATA) return;

        List<Map<String, String>> fileDataList = new ArrayList<>();

        // 获取数据
        getResourceFileData(fileDataList, configSet);

        // 导入数据
        importFileData(fileDataList);
    }


    private static void getResourceFileData(List<Map<String, String>> dataList, CdConfigSetting configSet) {
        if(IS_INIT_DATA) return;
        IS_INIT_DATA = true;

        CdResource cdResource = new CdResource();
        Properties properties = cdResource.readResource(configSet);
        Map<String, String> map;
        if (properties.isEmpty()) return;

        for (Object o : properties.keySet()) {
            String key = o.toString();
            map = new HashMap<>();
            map.put(CONST_CD_KEY, key);
            map.put(CONST_CD_VALUE, properties.getProperty(key));
            dataList.add(map);
        }

        System.out.println("<<<<< CD-DROPDOWN 资源文件完成加载 ^-^  >>>>>");
    }

    private static void importFileData(List<Map<String, String>> fileDataList) {
        if (null == fileDataList || fileDataList.isEmpty()) return;
        int sequence = 0;
        for (Map<String, String> dataMap : fileDataList) {
            putCdData(dataMap.get(CONST_CD_KEY), dataMap.get(CONST_CD_VALUE), ++sequence);
        }
    }

    private static void putCdData(String key, String value, int seq) {

        CdCheckResult cdCheckResult = CdLogicCheckUtil.checkKey(key, value);
        if (cdCheckResult.isSuc()) {
            String[] keyArr = cdCheckResult.getKeyArr();
            String[] valueArr = cdCheckResult.getValueArr();
            setValueMap(valueArr, getValueMap(keyArr), seq);
        } else {
            System.out.println("数据格式错误 : key--" + key + " ; value--" + value);
        }
    }

    @SuppressWarnings("unchecked")
    private static Map<String, Object> getValueDataMap(String key, Map<String, Object> dataMap) {
        if (dataMap.containsKey(key)) {
            return (Map<String, Object>) dataMap.get(key);
        }
        Map<String, Object> newMap = new ConcurrentHashMap<>();
        dataMap.put(key, newMap);
        return newMap;
    }

    private static Map<String, Object> getNameSpaceMap(String[] keyArr) {
        return getValueDataMap(keyArr[0], cdCoreDataMap);
    }

    private static Map<String, Object> getTypeMap(String[] keyArr) {
        return getValueDataMap(keyArr[1], getNameSpaceMap(keyArr));
    }

    private static Map<String, Object> getValueMap(String[] keyArr) {
        return getValueDataMap(keyArr[2], getTypeMap(keyArr));
    }

    private static void setValueMap(String[] valueArr, Map<String, Object> valueMap, int seq) {
        valueMap.put(CONST_VAL_DATA_KEY, valueArr[0]);
        valueMap.put(CONST_VAL_COMMENT_KEY, (valueArr.length >= 2 ? valueArr[1] : ""));
        valueMap.put(CONST_VAL_ORIGINAL_SEQ_KEY, seq);
    }

    public static Map<String, Object> getAllConstData() {
        return cdCoreDataMap;
    }

}
